<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<style>
    table tr th, table tr td {
        border-color: #000000;
        text-align: center;
        vertical-align: middle;
        font-size: 16px;
        line-height: 30px;
        border: #000000 1px solid;
    }

    table tr td input {
        border: none;
        width: 100%;
        height: 30px;
        outline: none;
        text-align: center;
    }

    table tr td textarea {
        border: none;
        width: 100%;
        height: 30px;
        outline: none;
        resize: none;

    }

    .changeCursorIco {
        cursor: pointer !important;
    }
</style>
<body class="hold-transition skin-yellow sidebar-mini">
<!--参与部门  模态框-->
<div class="modal fade" id="modal-canYuRen-select">
    <div class="modal-dialog" style="width:800px;height: 1000px">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">选择部门</h4>
            </div>
            <div class="modal-body">
                <form id="branchForm">
                    <div>
                        <button type="button" style="margin-bottom: 5px" class="btn btn-info pull-left"
                                id="btn-submit-CanYuRen1">添加
                        </button>
                        <table class="table table-bordered" id="bootstrap-table1"></table>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box" style="box-shadow:none;border:none;">
                <div class="box-body">
                    <div class="col-md-12" style="padding-bottom: 25px;">
                        <button class="btn btn-default pull-right" style="width: 82px;"
                                onclick="javascript:window.close()" type="button">关闭
                        </button>
                        <button id="btn_scheduling_complete" class="btn btn-success pull-right"
                                shiro:hasPermission="pro:other:edit"
                                style="margin-right: 5px;"
                                th:data-prodOrdNo="${prodOrd.prodNo}" onclick="saveCommonSchedulingComplete(this)"
                                type="button">保存
                        </button>
                    </div>
                    <div class="col-md-12" style="padding-left: 205px;padding-right: 145px;">
                        <form id="dataForm">
                            <table id="formTable" width="100%" border="0" cellpadding="0" cellspacing="0"
                                   style="z-index: -2">
                                <tr id="tableTitle1">
                                    <td colspan="7"
                                        style="font-size: 20px;height: 35px">
                                        <B>其他类项目排产</B>
                                    </td>
                                    <td style="border:none">
                                    </td>
                                </tr>
                                <tr id="prodInfo">
                                    <td style="width: 100px;">项目编号</td>
                                    <td style="width:200px;" th:text="${prodOrd.prodNo}"></td>
                                    <td style="width:200px;">项目名称</td>
                                    <td style="width:200px;" th:text="${prodOrd.prodName}"></td>
                                    <td style="width:200px;">数量</td>
                                    <td th:text="${prodOrd.prodNum}" colspan="2"></td>
                                    <td style="border:none">
                                    </td>
                                </tr>
                                <tr id="proInfo">
                                    <td>安装地点</td>
                                    <td th:text="${prodOrd.insSite}"></td>
                                    <td>需方</td>
                                    <td th:text="${prodOrd.customer}"></td>
                                    <td>交货时间</td>
                                    <td th:text="${#dates.format(prodOrd.prodDelDat, 'yyyy-MM-dd')}" colspan="2"></td>
                                    <td style="border:none">
                                    </td>
                                </tr>
                                <tr id="tableTitle2">
                                    <td colspan="7"
                                        style="font-size: 20px;height: 35px">
                                        <B>项目排产</B>
                                    </td>
                                    <td style="border:none">
                                    </td>
                                </tr>
                                <tr id="tableTitle3">
                                    <td>序号</td>
                                    <td>任务节点名称</td>
                                    <td>计划开始时间</td>
                                    <td>计划结束时间</td>
                                    <td>所属部门</td>
                                    <td>派工工时</td>
                                    <td>备注</td>
                                    <td name="oper" style="border:none;width:60px;">
                                        <button id="btn_addRow" type="button" class="pull-left" onclick="addRow(this)"
                                                style="border:none;background:transparent;color:green;font-size: 18px">
                                            <i class="fa fa-plus"></i></button>
                                    </td>
                                </tr>
                            </table>
                        </form>
                    </div>
                    <div class="col-md-10 col-md-offset-1"
                         style="font-size: 16px;font-family:'Source Sans Pro','Helvetica Neue',Helvetica,Arial,sans-serif;line-height: 30px;">
                        <div class="form-inline">
                            <div class="form-group pull-left">
                                <label class="control-label">
                                    创建人：
                                </label>
                                <label id="fou" th:text="${fou}" class="control-label"></label>
                            </div>
                            <div class="form-group pull-right" style="margin-right: 60px;">
                                <label class="control-label">
                                    创建时间：
                                </label>
                                <label id="creDat" th:text="${creDat}" class="control-label"></label>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">
    let planData, planOptionList, delPrpdIdArray = new Array(), deptList, deptOptionList,
        publicProdNo = [[${prodOrd.prodNo}]];
    let isHaveProduction = false, rouCount = 1;
    var xinZeng = "xinZeng";
    var canYuCount = 0; //每次添加参与部门  计数+1   删除参与人计数-1   用此判断是否添加了部门
    //当前控件ID
    var currentDeptControlId = "";
    //添加的ID
    var addId = "";
    /**
     * Jquery 就绪函数
     */
    $(function () {
        //加载计划数据
        loadPlanTemplateData();
        /**增加参与部门*/
        $('#dataForm').on('click', '.out-add', function () {
            currentDeptControlId = "#" + $(this).attr("sn");
            addId = "#" + $(this).attr("id");
            js.modal.open("modal-canYuRen-select");
            loadDepts();
        });
        /**移除参与部门*/
        $('#dataForm').on('click', '.out-del', function () {
            //找到要删除的人的 父级 span  id
            var canYuBranchName = $(this).parent().attr('id');
            //删除隐藏input中 branchid
            var delCanYuBranchName = "";
            if (canYuBranchName.indexOf(xinZeng) != -1) {
                delCanYuBranchName = canYuBranchName.slice(7);
            } else {
                delCanYuBranchName = canYuBranchName;
            }
            delCanYuBranchName = delCanYuBranchName.substring(0, delCanYuBranchName.indexOf("#"));

            var idValue = $($(this).attr("sn")).val();
            var BranchIdList = idValue.split(",");
            for (let i = 0; i < BranchIdList.length; i++) {
                if (BranchIdList[i] === delCanYuBranchName) {
                    BranchIdList.splice(i, 1);
                }
            }
            $($(this).attr("sn")).val(BranchIdList.toString());
            canYuCount--;//每次添加参与人  计数+1   删除参与人计数-1   用此判断是否添加了参与部门
            //删除部门 span
            var box = document.getElementById(canYuBranchName);
            box.remove();
        });
        /**添加参与部门*/
        $("#btn-submit-CanYuRen1").click(function () {
            var branchNameList = $(currentDeptControlId).val();
            var branchNameListArray = branchNameList.split(",");
            $('input[name="checkbox_select"]:checkbox:checked').each(function (i, value, id) {
                var branchName = $(this).attr("id");
                for (let j = 0; j < branchNameListArray.length; j++) {
                    if (branchNameListArray[j] === branchName) {
                        js.modal.alert("请勿重复添加！");
                        return;
                    }
                }
                add(branchName);
            });
            js.modal.hide('modal-canYuRen-select');
        });
    });

    /**
     * @Description: 添加部门
     * @Author: wsg
     **/
    function add(branchName) {
        var xinZengClass = xinZeng + branchName;
        //参与人后添加 人名和叉号
        $(addId).before(`
              <span id="` + xinZengClass + currentDeptControlId + `" style="display: block">` + branchName + `<span class="out-del" style="cursor:pointer;" sn="` + currentDeptControlId + `" title="删除" ><i class="fa fa-remove text-red"></i> </span></span>
            `);
        //隐藏input框填 科室id  用 ，分隔
        var temp1 = $(currentDeptControlId).val();
        var temp2 = temp1 + "," + branchName;
        if (temp1 === null || temp1 === "" || temp1 === undefined) {
            $(currentDeptControlId).val(temp2.slice(1));
        } else {
            $(currentDeptControlId).val(temp2);
        }
        js.modal.hide("modal-canYuRen-select")
        canYuCount++; //每次添加参与人  计数+1   删除参与人计数-1   用此判断是否添加了部门
    }

    function loadDepts() {
        /** 加载部门列表 */
        $('#bootstrap-table1').bootstrapTable('destroy');
        table1 = js.table.init({
            id: "bootstrap-table1",
            url: ctx + 'task/scheduling/getDeptList2',
            showColumns: false,
            showExport: false,
            pageSize: 300,
            pagination: false,
            columns: [
                {
                    title: '<input class="checkboxAll"  type="checkbox" onclick="selectedAll()" >', width: '40',
                    formatter: function (value, row, index) {
                        return '<input class="checkbox_select"  type="checkbox" name= "checkbox_select" ' +
                            'value= "' + $('<div/>').text(row.id).html() + '" id="' + $('<div/>').text(row.name).html() + '"    >';
                    }
                },
                {
                    title: '序号', field: 'id', width: '50',
                    formatter: function (value, row, index, field) {
                        return index + 1;
                    }
                },
                {title: "部门名称", field: 'name'}
            ]
        });
    }

    /**
     * 添加新行
     */
    function addRow(obj) {
        let tdEl = new Array();
        let newOrderNo = getNewOrderNo();
        tdEl.push('<td name="orderNo">' + newOrderNo + '</td>');
        tdEl.push('<td><div class="list-name-input">' + planOptionList + '</div></td>');
        tdEl.push('<td><input class="changeCursorIco" name="prpdPlaStaDat" type="text" style="background:#FEFFC3;outline：none;" readOnly="readonly" alt="点击选择开始时间"/></td>');
        tdEl.push('<td><input class="changeCursorIco" name="prpdPlaEndDat" type="text" style="background:#FEFFC3;outline：none;" readOnly="readonly" alt="点击选择结束时间"/></td>');
        //let deptSelect = "<select class='form-control' name='prpdDepId' style='border:none;outline：none;'>";
        // deptSelect += "<option value=''>&nbsp;</option>";
        // for (let i = 0; i < deptList.length; i++) {
        //     deptSelect += "<option value='" + deptList[i].id + "'>" + deptList[i].name + "</option>";
        // }
        // deptSelect += "</select>";
        tdEl.push('<td><span class="out-add" sn="deptId_' + rouCount + '" id="out-add_' + rouCount + '" title="添加" style="\n' +
            '                                                     text-align: right;\n' +
            '                                                     margin-left: 20px;\n' +
            '                                                     cursor:pointer;\n' +
            '                                                    ">\n' +
            '                                                    <i class="fa fa-plus text-green">\n' +
            '\n' +
            '                                                    </i>\n' +
            '                                        </span>\n' +
            '                                        <input id="deptId_' + rouCount + '" name="prpdDepId" placeholder="参与科室" type="hidden"></td>');
        tdEl.push('<td><input class="form-control" name="prpdAssWorHours" type="text" style="background:#FEFFC3;outline：none;" value=""/></td>');
        tdEl.push('<td><textarea class="form-control" name="prpdRem" type="text" maxlength="255" style="background:#FEFFC3;outline：none;border: none;width: 100%;height: 30px;outline: none;resize: none;"></textarea></td>');
        tdEl.push('<td name="oper" style="border:none;width:60px;"><button type="button" class="pull-left" onclick="addRow(this)" style="border:none;background:transparent;color:green;font-size: 18px"><i class="fa fa-plus"></i></button><button onclick="subtractRow(this)" style="border:none;background:transparent;color:red;font-size: 18px"> <i class="fa fa-close"></i></button></td>');
        $(obj).parent().parent().after('<tr id="dataRow_' + newOrderNo + '">' + tdEl + '</tr>')
        $("#formTable [name='prpdPlaStaDat']").datepicker({
            format: "yyyy-mm-dd",
            language: "zh-cn",
            autoclose: true,
            todayHighlight: true,
            clearBtn: true
        });
        $("#formTable [name='prpdPlaEndDat']").datepicker({
            format: "yyyy-mm-dd",
            language: "zh-cn",
            autoclose: true,
            todayHighlight: true,
            clearBtn: true
        });
        $(obj).parent().html('');
        if (isHaveProduction === false) {
            $("#dataRow_" + newOrderNo).find("[name='prpdNamSelect']").val('生产完成时间').change();
            isHaveProduction = true;
        }
        rouCount++;
    }

    /**
     * 移除本行
     */
    function subtractRow(obj) {
        if ($(obj).parent().parent().find("[name='prpdId']").val() != null && $(obj).parent().parent().find("[name='prpdId']").val() != '') {
            delPrpdIdArray.push($(obj).parent().parent().find("[name='prpdId']").val());
        }
        $(obj).parent().parent().remove();
        let lastId = $('#formTable tr:last').attr('id');
        if (lastId === 'tableTitle3') {
            $('#formTable tr:last').find("[name='oper']").html('<button type="button" class="pull-left" onclick="addRow(this)" style="border:none;background:transparent;color:green;font-size: 18px"><i class="fa fa-plus"></i></button>');
        } else {
            $('#formTable tr:last').find("[name='oper']").html('<button type="button" class="pull-left" onclick="addRow(this)" style="border:none;background:transparent;color:green;font-size: 18px"><i class="fa fa-plus"></i></button><button onclick="subtractRow(this)" style="border:none;background:transparent;color:red;font-size: 18px"> <i class="fa fa-close"></i></button>');
        }
        //重新排序
        let index = 1;
        $("#formTable tr [name='orderNo']").each(function () {
            $(this).html(index);
            index++;
        });
    }

    /**
     * 获取最新行号
     */
    function getNewOrderNo() {
        let rowIndex = 1;
        $("#formTable tr").each(function () {
            let idText = $(this).attr("id");
            if (idText.indexOf("dataRow") > -1) {
                rowIndex++;
            }
        });
        return rowIndex;
    }

    /**
     * 验证当结束时间是否大于开始时间
     */
    function verificationStart(obj) {
        let startVal = $(obj).val();
        let endVal = $(obj).parent().parent().find("[name='planEndTime']").val();
        if (startVal != undefined && endVal != undefined) {
            let start = new Date(startVal);
            let end = new Date(endVal);
            if (end <= start) {
                $(obj).val('');
                js.modal.warning("尊敬的用户，您所输入的开始时间必须大于结束时间！");
            }
        }
    }
    /**
     *全选
     */
    function selectedAll() {
        if ($('.checkboxAll').is(":checked")) {
            $('.checkbox_select').each(function () {
                this.checked = true;
            });
        } else {
            $('.checkbox_select').each(function () {
                this.checked = false;
            });
        }
    }
    /**
     * 验证当结束时间是否大于开始时间
     */
    function verificationEnd(obj) {
        let endVal = $(obj).val();
        let startVal = $(obj).parent().parent().find("[name='planStartTime']").val();
        if (startVal != undefined && endVal != undefined) {
            let end = new Date($(obj).val());
            let start = new Date($(obj).parent().parent().find("[name='planStartTime']").val());
            if (end <= start) {
                $(obj).val('');
                js.modal.warning("尊敬的用户，您所输入的结束时间必须大于开始时间！");
            }
        }
    }

    /**
     * 排产
     */
    function saveCommonSchedulingComplete(obj) {
        let dataArray = new Array();
        let dataModel = new Object();
        let warningInfo = '';
        let index = 1;
        let warningInfo2 = '';
        let index2 = 1;
        let prpdNamArray = new Array();
        let warningInfoEndDat = '';
        let indexEndDat = 1;
        let warningInfoDep = '';
        let indexEndDep = 1;
        $.each($("#dataForm").serializeArray(), function (_, kv) {
            if (kv.name === 'prpdNam') {
                if (kv.value === '') {
                    warningInfo += "第" + index + "行、";
                    index++
                } else if (prpdNamArray.indexOf(kv.value) > -1) {
                    warningInfo2 += "第" + index2 + "行、";
                    index2;
                } else if (prpdNamArray.indexOf(kv.value) === -1) {
                    prpdNamArray.push(kv.value);
                }
            }
            if (kv.name === 'prpdPlaEndDat') {
                if (kv.value === "") {
                    warningInfoEndDat += "第" + indexEndDat + "行、";
                    indexEndDat++;
                }
            }
            if (kv.name === 'prpdDep') {
                if (kv.value === "") {
                    warningInfoDep += "第" + indexEndDep + "行、";
                    indexEndDep++;
                }
            }
        });
        if (warningInfo.length > 0 || warningInfo2.length > 0) {
            if (warningInfo.length > 0) {
                warningInfo = warningInfo.substring(0, warningInfo.length - 1);
                js.modal.warning("尊敬的用户，请选择" + warningInfo + "任务节点名称！");
            }
            if (warningInfo2.length > 0) {
                warningInfo2 = warningInfo2.substring(0, warningInfo2.length - 1);
                js.modal.warning("尊敬的用户，您选择" + warningInfo2 + "任务节点名称出现重复，请修改！");
            }
            if (warningInfoEndDat.length > 0) {
                warningInfoEndDat = warningInfoEndDat.substring(0, warningInfoEndDat.length - 1);
                js.modal.warning("尊敬的用户，请选择" + warningInfoEndDat + "结束时间！");
            }
            if (warningInfoDep.length > 0) {
                warningInfoDep = warningInfoDep.substring(0, warningInfoDep.length - 1);
                js.modal.warning("尊敬的用户，请填写" + warningInfoDep + "所属部门！");
            }
            return;
        }
        let indexNo = 0;
        $.each($("#dataForm").serializeArray(), function (_, kv) {
            if (kv.name === 'prpdNam') {
                dataModel.prpdNam = kv.value;
            }
            if (kv.name === 'prpdCod') {
                if (kv.value != "") {
                    dataModel.prpdCod = kv.value;
                } else {
                    dataModel.prpdCod = null;
                }
            }
            if (kv.name === 'prpdOrdNo') {
                if (kv.value != "") {
                    dataModel.prpdOrdNo = kv.value;
                } else {
                    dataModel.prpdOrdNo = indexNo;
                    indexNo++;
                }
            }
            if (kv.name === 'prpdPlaStaDat') {
                if (kv.value != "") {
                    dataModel.prpdPlaStaDat = kv.value;
                } else {
                    dataModel.prpdPlaStaDat = null;
                }
            }
            if (kv.name === 'prpdPlaEndDat') {
                if (kv.value != "") {
                    dataModel.prpdPlaEndDat = kv.value;
                } else {
                    dataModel.prpdPlaEndDat = null;
                }
            }
            if (kv.name === 'prpdDepId') {
                if (kv.value != "") {
                    dataModel.prpdDepId = kv.value;
                } else {
                    dataModel.prpdDepId = null;
                }
            }
            if (kv.name === 'prpdAssWorHours') {
                if (kv.value != "") {
                    dataModel.prpdAssWorHours = kv.value;
                } else {
                    dataModel.prpdAssWorHours = null;
                }
            }
            if (kv.name === 'prpdRem') {
                if (kv.value != "") {
                    dataModel.prpdRem = kv.value;
                } else {
                    dataModel.prpdRem = null;
                }
                if (dataModel.prpdNam != undefined) {
                    dataArray.push(dataModel);
                    dataModel = new Object();
                }
            }
        });
        if (dataArray.length > 0) {
            js.post({
                url: ctx + 'task/common/saveSchedulingPlanAndComplete',
                type: 'post',
                traditional: true,            // 阻止深度序列化， 使参数可以使用数组
                dataType: 'json',
                contentType: "application/x-www-form-urlencoded",
                data: {
                    prodNo: $(obj).attr('data-prodOrdNo'),
                    prodPlanListJson: JSON.stringify(dataArray),
                    delPrpdIdArrayJson: JSON.stringify(delPrpdIdArray)
                },
                async: true,                // 异步执行
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        window.opener.reloadTable();
                        window.open('', '_self');
                        window.close();
                    } else {
                        js.modal.warning(result.msg);
                    }
                },
                error: function () {
                    js.modal.fail();
                }
            });
        } else {
            js.modal.warning("尊敬的用户，请添加排产计划！");
            return;
        }
    }



    /**
     * 加载计划数据
     */
    function loadPlanTemplateData() {
        js.post({
            url: ctx + 'task/common/loadPlanTemplateData',
            type: 'post',
            traditional: true,            // 阻止深度序列化， 使参数可以使用数组
            dataType: 'json',
            contentType: "application/x-www-form-urlencoded",
            data: {},
            async: true,                // 异步执行
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    planData = result.data;
                    planOptionList = '<div style="position:relative;"><span style="margin-left:30px;height:100%;width:30px;overflow:hidden;"><select name="prpdNamSelect" style="width:180px;margin-left:-40px;border: none;outline: none;background:#FEFFC3;" onchange="updRelDat(this)">';
                    planOptionList += "<option value=''>--请选择--</option>";
                    for (let i = 0; i < planData.length; i++) {
                        planOptionList += "<option data-propCod='" + planData[i].prpdCod + "' data-prpdOrdNo='" + planData[i].prpdOrdNo + "' value='" + planData[i].prpdNam + "'>" + planData[i].prpdNam + "</option>";
                    }
                    planOptionList += "</select ></span>";
                    planOptionList += "<input style='width:170px;position:absolute;left:0px;background:#FEFFC3;' name='prpdNam' maxlength='255' oninput='clearRelDat(this)'/>";
                    planOptionList += "<input name='prpdCod' type='hidden' style='display: none;' value='' />";
                    planOptionList += "<input name='prpdOrdNo' type='hidden' style='display: none' value='' />";
                    planOptionList += "<input name='prpdId' type='hidden' style='display: none' value='' /></div>";
                    js.post({
                        url: ctx + 'task/common/loadExistPlanData',
                        type: 'post',
                        traditional: true,            // 阻止深度序列化， 使参数可以使用数组
                        dataType: 'json',
                        contentType: "application/x-www-form-urlencoded",
                        data: {
                            prodOrdNo: $("#btn_scheduling_complete").attr('data-prodordno')
                        },
                        async: true,                // 异步执行
                        success: function (result) {
                            if (result.type === web_status.SUCCESS) {
                                let list = result.data;
                                if (list.length > 0) {
                                    $("#tableTitle3 [name='oper']").html('');
                                }
                                for (let i = 0; i < list.length; i++) {
                                    let prpdCod;
                                    if (list[i].prpdCod != null) {
                                        prpdCod = list[i].prpdCod;
                                        if (list[i].prpdCod === 'p9') {
                                            isHaveProduction = true;
                                        }
                                    } else {
                                        prpdCod = '';
                                    }
                                    let prpdNam;
                                    if (list[i].prpdNam != null) {
                                        prpdNam = list[i].prpdNam;
                                    } else {
                                        prpdNam = '';
                                    }
                                    let prpdOrdNo;
                                    if (list[i].prpdOrdNo != null) {
                                        prpdOrdNo = list[i].prpdOrdNo;
                                    } else {
                                        prpdOrdNo = '';
                                    }
                                    let prpdId;
                                    if (list[i].prpdId != null) {
                                        prpdId = list[i].prpdId;
                                    } else {
                                        prpdId = '';
                                    }
                                    let prpdPlaStaDat;
                                    if (list[i].prpdPlaStaDat != null) {
                                        prpdPlaStaDat = list[i].prpdPlaStaDat;
                                    } else {
                                        prpdPlaStaDat = '';
                                    }
                                    let prpdPlaEndDat;
                                    if (list[i].prpdPlaEndDat != null) {
                                        prpdPlaEndDat = list[i].prpdPlaEndDat;
                                    } else {
                                        prpdPlaEndDat = '';
                                    }
                                    let prpdDepId;
                                    if (list[i].prpdDepId != null) {
                                        prpdDepId = list[i].prpdDepId;
                                    } else {
                                        prpdDepId = '';
                                    }
                                    let prpdAssWorHours;
                                    if (list[i].prpdAssWorHours != null) {
                                        prpdAssWorHours = list[i].prpdAssWorHours;
                                    } else {
                                        prpdAssWorHours = '';
                                    }
                                    let prpdRem;
                                    if (list[i].prpdRem != null) {
                                        prpdRem = list[i].prpdRem;
                                    } else {
                                        prpdRem = '';
                                    }
                                    let planOptionListEml = '<div style="position:relative;"><span style="background:#FEFFC3;margin-left:30px;height:100%;width:30px;overflow:hidden;"><select style="width:180px;margin-left:-40px;border: none;outline: none" onchange="updRelDat(this)">';
                                    // let planOptionListEml = '<select class="form-control" name="prpdNam" onchange="updRelDat(this)" style="border:none;outline：none;">';
                                    planOptionListEml += "<option value=''>--请选择--</option>";
                                    for (let j = 0; j < planData.length; j++) {
                                        let selected = "";
                                        if (list[i].prpdCod === planData[j].prpdCod) {
                                            selected = "selected='selected'";
                                            if (list[i].prpdCod != null) {
                                                prpdCod = list[i].prpdCod;
                                            } else {
                                                prpdCod = "";
                                            }
                                            if (list[i].prpdNam != null) {
                                                prpdNam = list[i].prpdNam;
                                            } else {
                                                prpdNam = "";
                                            }
                                            if (list[i].prpdOrdNo != null) {
                                                prpdOrdNo = list[i].prpdOrdNo;
                                            } else {
                                                prpdOrdNo = "";
                                            }
                                            if (list[i].prpdId != null) {
                                                prpdId = list[i].prpdId;
                                            } else {
                                                prpdId = "";
                                            }
                                            if (list[i].prpdPlaStaDat != null) {
                                                prpdPlaStaDat = list[i].prpdPlaStaDat;
                                            } else {
                                                prpdPlaStaDat = "";
                                            }
                                            if (list[i].prpdPlaEndDat != null) {
                                                prpdPlaEndDat = list[i].prpdPlaEndDat;
                                            } else {
                                                prpdPlaEndDat = "";
                                            }
                                            if (list[i].prpdDepId != null) {
                                                prpdDepId = list[i].prpdDepId;
                                            } else {
                                                prpdDepId = "";
                                            }
                                            if (list[i].prpdRem != null) {
                                                prpdRem = list[i].prpdRem;
                                            } else {
                                                prpdRem = "";
                                            }

                                        }
                                        planOptionListEml += "<option " + selected + " data-propCod='" + planData[j].prpdCod + "' data-prpdOrdNo='" + planData[j].prpdOrdNo + "' value='" + planData[j].prpdNam + "'>" + planData[j].prpdNam + "</option>";
                                    }
                                    planOptionListEml += "</select></span>";
                                    planOptionListEml += "<input style='background:#FEFFC3;width:170px;position:absolute;left:0px;' name='prpdNam' maxlength='255' oninput='clearRelDat(this)' value='" + prpdNam + "'/>";
                                    planOptionListEml += "<input name='prpdCod' type='hidden' style='display: none' value='" + prpdCod + "' />";
                                    planOptionListEml += "<input name='prpdOrdNo' type='hidden' style='display: none' value='" + prpdOrdNo + "' />";
                                    planOptionListEml += "<input name='prpdId' type='hidden' style='display: none' value='" + prpdId + "' /></div>";
                                    let tdEl = new Array();
                                    let newOrderNo = getNewOrderNo();
                                    tdEl.push('<td name="orderNo">' + newOrderNo + '</td>');
                                    tdEl.push('<td>' + planOptionListEml + '</td>');
                                    tdEl.push('<td><input class="form-control changeCursorIco" name="prpdPlaStaDat" type="text" style="background:#FEFFC3;outline：none;" readOnly="readonly" value="' + prpdPlaStaDat + '" alt="点击选择开始时间"/></td>');
                                    tdEl.push('<td><input class="form-control changeCursorIco" name="prpdPlaEndDat" type="text" style="background:#FEFFC3;outline：none;" readOnly="readonly" value="' + prpdPlaEndDat + '" alt="点击选择结束时间"/></td>');
                                    let deptHtml = '';
                                    if (prpdDepId != '' && prpdDepId.indexOf(",") != -1) {
                                        let deptArray = prpdDepId.split(',');
                                        for (let j = 0; j < deptArray.length; j++) {
                                            deptHtml+='<span id="'+deptArray[j]+'#deptId_'+rouCount+'" ' +
                                                'style="display: block">' +
                                                deptArray[j] +
                                                '<span class="out-del" ' +
                                                'style="cursor:pointer;" ' +
                                                'sn="#deptId_'+rouCount+'" ' +
                                                'title="删除">' +
                                                '<i class="fa fa-remove text-red">' +
                                                '</i> ' +
                                                '</span>' +
                                                '</span>';
                                        }
                                    } else if (prpdDepId != '' && prpdDepId.indexOf(",") == -1) {
                                        deptHtml='<span id="'+prpdDepId+'#deptId_'+rouCount+'" ' +
                                                        'style="display: block">' +
                                                      prpdDepId +
                                                     '<span class="out-del" ' +
                                                            'style="cursor:pointer;" ' +
                                                            'sn="#deptId_'+rouCount+'" ' +
                                                            'title="删除">' +
                                                        '<i class="fa fa-remove text-red">' +
                                                        '</i> ' +
                                                     '</span>' +
                                                '</span>';
                                    }
                                    tdEl.push('<td>'+deptHtml+'<span class="out-add" sn="deptId_' + rouCount + '" id="out-add_' + rouCount + '" title="添加" style="\n' +
                                        '                                                     text-align: right;\n' +
                                        '                                                     margin-left: 20px;\n' +
                                        '                                                     cursor:pointer;\n' +
                                        '                                                    ">\n' +
                                        '                                                    <i class="fa fa-plus text-green">\n' +
                                        '\n' +
                                        '                                                    </i>\n' +
                                        '                                        </span>\n' +
                                        '                                        <input id="deptId_' + rouCount + '" name="prpdDepId" placeholder="参与科室" type="hidden" value="'+prpdDepId+'"></td>');




                                    tdEl.push('<td><input class="form-control" name="prpdAssWorHours" type="text" style="background:#FEFFC3;outline：none;" value="' + prpdAssWorHours + '"/></td>');
                                    tdEl.push('<td><textarea class="form-control" name="prpdRem" type="text" maxlength="255" style="background:#FEFFC3;width: 100%;height: 100%;outline：none;border: none;width: 100%;height: 30px;outline: none;resize: none;">' + prpdRem + '</textarea></td>');
                                    if (i === list.length - 1) {
                                        tdEl.push('<td name="oper" style="border:none;width:60px;"><button type="button" onclick="addRow(this)" style="border:none;background:transparent;color:green;font-size: 18px"><i class="fa fa-plus"></i></button><button onclick="subtractRow(this)" style="border:none;background:transparent;color:red;font-size: 18px"> <i class="fa fa-close"></i></button></td>');
                                    } else {
                                        tdEl.push('<td name="oper" style="border:none;width:60px;"><button onclick="subtractRow(this)" style="border:none;background:transparent;color:red;font-size: 18px;margin-left:25.84px;"> <i class="fa fa-close"></i></button></td>');
                                    }
                                    $("#formTable").append('<tr id="dataRow_' + newOrderNo + '">' + tdEl + '</tr>')
                                    $("#formTable [name='prpdPlaStaDat']").datepicker({
                                        format: "yyyy-mm-dd",
                                        language: "zh-cn",
                                        autoclose: true,
                                        todayHighlight: true,
                                        clearBtn: true
                                    });
                                    $("#formTable [name='prpdPlaEndDat']").datepicker({
                                        format: "yyyy-mm-dd",
                                        language: "zh-cn",
                                        autoclose: true,
                                        todayHighlight: true,
                                        clearBtn: true
                                    });
                                    rouCount++;
                                }
                                if (isHaveProduction === false) {
                                    $("#btn_addRow").click();
                                }
                            }
                        },
                        error: function () {
                            js.modal.fail();
                        }
                    });
                    // js.post({
                    //     url: ctx + 'task/common/loadDeptLis',
                    //     type: 'post',
                    //     traditional: true,            // 阻止深度序列化， 使参数可以使用数组
                    //     dataType: 'json',
                    //     contentType: "application/x-www-form-urlencoded",
                    //     data: {},
                    //     async: true,                // 异步执行
                    //     success: function (result) {
                    //         if (result.type === web_status.SUCCESS) {
                    //             deptList = result.data;
                    //
                    //         }
                    //     }
                    // });

                }
            },
            error: function () {
                js.modal.fail();
            }
        });
    }

    /**
     * 更新相关数据
     * @param obj
     */
    function updRelDat(obj) {
        $(obj).parent().parent().find("[name='prpdNam']").val($(obj).find('option:selected').text());
        $(obj).parent().parent().find("[name='prpdCod']").val($(obj).find('option:selected').attr('data-propCod'));
        $(obj).parent().parent().find("[name='prpdOrdNo']").val($(obj).find('option:selected').attr('data-prpdOrdNo'));
    }

    /**
     * 如果输入框内容改变则清空码和生产令
     * 如果内容与计划名完全相同则把匹配到数据赋在控件上
     * @param obj
     */
    function clearRelDat(obj) {
        $(obj).parent().parent().find("[name='prpdCod']").val(null);
        let thisVal = $(obj).val();
        for (let i = 0; i < planData.length; i++) {
            if (thisVal === planData[i].prpdNam) {
                $(obj).parent().parent().find("[name='prpdCod']").val(planData[i].prpdCod);
                $(obj).parent().parent().find("[name='prpdOrdNo']").val(planData[i].prpdOrdNo);
                break;
            }
        }
    }
</script>
</body>
</html>